//! return 语句

use crate::lexer::token::Token;
use crate::parser::expression::Expression;
use crate::parser::node::{Node, NodeRoot};
use crate::parser::statement::Statement;
use std::ops::Deref;

#[derive(Debug, Clone)]
pub struct ReturnStatement {
    node: NodeRoot,
    token: Token,
    expression: Box<dyn Expression>,
}

impl ReturnStatement {
    pub fn new(token: Token, expression: Box<dyn Expression>) -> Self {
        let l = format!("return with {}", expression.cast_to_node().get_literal());
        let node = NodeRoot::new(l, "ReturnStatement".to_string());
        Self {
            node,
            token,
            expression,
        }
    }

    pub fn expression(&self) -> &dyn Expression {
        self.expression.as_ref()
    }
}

impl Statement for ReturnStatement {
    fn cast_to_node(&self) -> &dyn Node {
        &self.node
    }
}

impl Deref for ReturnStatement {
    type Target = NodeRoot;

    fn deref(&self) -> &Self::Target {
        &self.node
    }
}
